//后台服务器地址
export const BASE_URL = "/"

export const ajax = (option) => {
	const user_info = uni.getStorageSync('user_info');
	option.data = option.data || {};
	option.data.token = user_info.token || 0;
	return new Promise((resolve, reject) => {
		
		uni.showLoading({
			title: '加载中'
		});
		uni.request({
			timeout: 1500,
			url: BASE_URL + option.url,
			method: option.method || "GET",
			header: {
				"Content-Type": 'application/x-www-form-urlencoded'
			},
			data: option.data || {},
			success: (res) => {
				uni.hideLoading()
				uni.stopPullDownRefresh()
				if (res.data.status == 1) {
					resolve(res.data)
					return
				}
				if (res.data.status == 2) {
					uni.hideTabBarRedDot({
						index: 2,
					})
					uni.clearStorageSync();
					uni.showToast({
						title: res.data.msg,
						success() {
							setTimeout(function() {
								uni.navigateTo({
									url: '/pages/login/index'
								})
							}, 1000);
						}
					});
					return
				}
				resolve(res.data)
			},
			fail: (err) => {
				uni.hideLoading()
				uni.$u.toast('服务器请求失败') // uview简洁写法，需要提前引入uview组件
				reject(err)
			}
		})
	})
}